package com.txsj.scan.presenter;

import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.BaseResult;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.bean.WarehouseBean;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.view.AllocationIView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AllocationPresenter extends BasePresenter<AllocationIView> {
    public AllocationPresenter(AllocationIView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }

    /**
     * 进行提交
     * @param warehouseBean  出库仓库
     * @param inWarehouseBean 入库仓库
     * @param epcGoodsInfos  商品列表
     * @param remakes  备注
     */
    public void commitGoods(WarehouseBean warehouseBean, WarehouseBean inWarehouseBean,
                            List<GoodsInfo> epcGoodsInfos, String remakes) {
        if (epcGoodsInfos.size() == 0)return;
        List<String> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        for (int i= 0;i<epcGoodsInfos.size();i++){
            for (int j = 0;j<epcGoodsInfos.get(i).getEpcList().size();j++){
                if (list.contains(epcGoodsInfos.get(i).getEpcList().get(j).getStrEpc())) {
                    view.onError("标签重复");
                    Trace.e("调拨重复的标签："+epcGoodsInfos.get(i).getEpcList().get(j).getStrEpc());
                    return;
                }
                list.add(epcGoodsInfos.get(i).getEpcList().get(j).getStrEpc());
                if (i == epcGoodsInfos.size() - 1 && j == epcGoodsInfos.get(i).getEpcList().size() - 1){
                    sb.append(epcGoodsInfos.get(i).getEpcList().get(j).getStrEpc());
                }else {
                    sb.append(epcGoodsInfos.get(i).getEpcList().get(j).getStrEpc()+",");
                }
            }
        }
        Map<String,String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID,""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID,0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN,""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("bill_remark",remakes);
        params.put("employee_id",PreferenceUtil.getInstance().getInt(PreferenceUtil.EMPLOYEEID,0)+"");
        params.put("employee_name",PreferenceUtil.getInstance().getString(PreferenceUtil.ROLE,""));
        params.put("epc_list",sb.toString());
        params.put("into_warehouse_id",inWarehouseBean.getWarehouse_id()+"");
        params.put("out_warehouse_id", warehouseBean.getWarehouse_id()+"");
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).commitAllocateGoods(RetrofitManager.getHeader(),params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResult<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResult<Object> result) {
                        Trace.e("wcj======"+result.getMsg());
                        if (result.isSuccess()){
                            view.onSuccess("调拨成功");
                        }else {
                            view.onError(result.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError("网络异常");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    /*
    35A01136300000000001F1FF%2C
    35A01136300020300001766D%2C
    35A01136300020300001766A%2C
    35A011363000203000017669%2C
    35A011363000000000005A62%2C
    35A0113630000000000059E2
     */
}
